package LinkedList.Medium;

import DataStructures.ListNode;

public class LC0019 {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode slow = head, fast = head;
        ListNode left = null; // slow左侧的结点
        for (int i = 1; i < n; i++) fast = fast.next;
        while (fast.next != null) {
            left = slow;
            slow = slow.next;
            fast = fast.next;
        }

        if (left == null) return slow.next; // 删除头结点

        left.next = slow.next;
        return head;
    }
}
